package com.bonc.tools;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * Created by zhangxinsong on 2019-03-25.
 */
@Component
public class DataLoad {

    private static final Logger logger = Logger.getLogger(DataLoad.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;


    private Connection conn = null;


    public static InputStream getDataInputStream(String fileName) {
        File file = new File(fileName);
        InputStream is = null;
        BufferedReader reader = null;
        StringBuffer builder = new StringBuffer();
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                builder.append(tempString);
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            byte[] bytes = builder.toString().getBytes();
            is = new ByteArrayInputStream(bytes);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

        return is;
    }

    public int bulkLoadFromInputStream(String loadDataSql, InputStream dataStream) throws SQLException {
        if (dataStream == null) {
            logger.info("InputStream is null ,No data is imported");

            return 0;
        }

        conn = jdbcTemplate.getDataSource().getConnection();

        PreparedStatement statement = conn.prepareStatement(loadDataSql);
        int result = 0;

        if (statement.isWrapperFor(com.mysql.jdbc.Statement.class)) {
            com.mysql.jdbc.PreparedStatement mysqlStatement = statement.unwrap(com.mysql.jdbc.PreparedStatement.class);

            mysqlStatement.setLocalInfileInputStream(dataStream);

        }

        return result;
    }

    /**
     * load bulk data from InFile to MySQL
     */
    public int bulkLoadFromInFile(String loadDataSql) throws SQLException {
        conn = jdbcTemplate.getDataSource().getConnection();

        PreparedStatement statement = conn.prepareStatement(loadDataSql);
        int result = 0;

        if (statement.isWrapperFor(com.mysql.jdbc.Statement.class)) {
            com.mysql.jdbc.PreparedStatement mysqlStatement = statement.unwrap(com.mysql.jdbc.PreparedStatement.class);
            result = mysqlStatement.executeUpdate(loadDataSql);
        }

        return result;
    }
    public int query(String loadDataSql) throws SQLException {
        conn = jdbcTemplate.getDataSource().getConnection();

        PreparedStatement statement = conn.prepareStatement(loadDataSql);
        int result = 0;

        if (statement.isWrapperFor(com.mysql.jdbc.Statement.class)) {
            com.mysql.jdbc.PreparedStatement mysqlStatement = statement.unwrap(com.mysql.jdbc.PreparedStatement.class);
            ResultSet rs = mysqlStatement.executeQuery(loadDataSql);
            if (rs.next()){
                result = rs.getInt(1);
            }
        }

        return result;
    }
}